﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using University.Workshop.Presenters.OtbData.Register;
using Microsoft.Win32;
using System.IO;

namespace University.Workshop.Presentation.OTBData.Register
{
    /// <summary>
    /// Interaction logic for PageFirstStepRegisterOtbData.xaml
    /// </summary>
    public partial class PageFirstStepRegisterOtbData : Page, IFirstStepForRegister
    {
        PresenterRegisterOtbData pInternal;
        PresenterRegisterFirstStep pRegisterFirstStep;
        public PageFirstStepRegisterOtbData(PresenterRegisterOtbData pExternal)
        {
            InitializeComponent();
            this.pInternal = pExternal;
            pRegisterFirstStep = new PresenterRegisterFirstStep(this);
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            App.Current.MainWindow.Title = "Registrar Datos OTB";
        }

        #region Fields
        public string Name
        {
            get
            {
                return OtbNameTxt.Text;
            }
            set
            {
                OtbNameTxt.Text = value;
            }
        }

        public string Managment
        {
            get
            {
                return managementTxt.Text;
            }

            set
            {
                managementTxt.Text = value;
            }
        }

        public int? ExpirationDay
        {
            get 
            {
                if (expirationDayTxt.Text == "")
                    return null;
                else
                    return Convert.ToInt32(expirationDayTxt.Text);
            }
            set
            {
                expirationDayTxt.Text = value.ToString();
            }
        }

        public string Background
        {
            get
            {
                return backgroundTxt.Text;
            }

            set
            {
                if (!string.IsNullOrWhiteSpace(value))
                    if (File.Exists(value))
                    {
                        backgroundTxt.Text = value;
                        BitmapImage image = new BitmapImage();
                        using (FileStream stream = File.OpenRead(value))
                        {
                            image.BeginInit();
                            image.StreamSource = stream;
                            image.CacheOption = BitmapCacheOption.OnLoad;
                            image.EndInit(); // load the image from the stream
                        } // close the stream
                        backgroundImg.Source = image;
                    }
                    else
                    {

                        backgroundTxt.Text = string.Empty;
                    }

            }
        }
        #endregion

        #region Error Message
        public string SystemMessage
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                MessageBox.Show(value);
            }
        }

        public string ErrorMessage
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                MessageBox.Show(value);
            }
        }
        #endregion

        #region Validations
        private void managementTxt_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            managmentCld.Visibility = Visibility.Visible;
        }

        private void expirationDayTxt_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            string temporal = ((TextBox)sender).Text + e.Text;
            foreach (char ch in e.Text)
            {
                e.Handled = !(char.IsDigit(ch));
            }
        }

        private void periodCalendar_DisplayModeChanged(object sender, CalendarModeChangedEventArgs e)
        {
            if (e.NewMode != CalendarMode.Year)
            {
                DateTime dt = ((Calendar)sender).DisplayDate;
                ((Calendar)sender).DisplayMode = CalendarMode.Year;
                ((Calendar)sender).DisplayDate = dt;
                ((Calendar)sender).SelectedDate = dt;
                managmentCld.Visibility = Visibility.Hidden;
            }
            if (managementTxt != null)
            {
                string year = managmentCld.DisplayDate.Year.ToString();
                string month = managmentCld.DisplayDate.Month.ToString();

                if (managmentCld.DisplayDate.Month < 10)
                    month = "0" + month;

                managementTxt.Text = string.Format("{1}-{0}", year, month);
            }
        }

        private void periodCalendar_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
        }

        private void OtbNameTxt_PreviewGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            managmentCld.Visibility = Visibility.Hidden;
        }
        #endregion


        public void UpdateViewData()
        {
            pRegisterFirstStep.UpdateViewData();
        }

        public void ChargeDataToBasic()
        {
            pRegisterFirstStep.ChargeOtbData();
        }

        public void GoNextPage()
        {
            pInternal.GoStepTwo();
        }

        public void GoBackPage()
        {

        }

        private void SearchBackgroundBtn_Click(object sender, RoutedEventArgs e)
        {
            string path;
            OpenFileDialog op = new OpenFileDialog();
            op.Title = "Buscar Fondo";
            op.Filter = "All supported graphics|*.jpg;*.jpeg;*.png|" +
                "JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" +
                "Portable Network Graphic (*.png)|*.png";
            if (op.ShowDialog() == true)
            {
                path = op.InitialDirectory + op.FileName;
                pRegisterFirstStep.LoadImage(path);
            }
            path = string.Empty;        
        }

        public Model.OtbData OtbDataToRegister
        {
            get;
            set;
        }

    }
}
